<?php

declare(strict_types=1);

namespace App\AdminApi\Modular\Administer\Controller;

use App\AdminApi\BasicsController;
use App\AdminApi\Modular\Administer\Service\DepartmentService;
use App\AdminApi\Modular\Administer\Service\UserService;
use App\AdminApi\Modular\Administer\Validate\DepartmentValidate;
use App\Common\Extend\Guard\Auth;
use Core\HyperfUnit\Response;
use Core\Package\ElementPlus\FormCreate\ElemenuForm;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\{Controller, GetMapping, PostMapping, RequestMapping};
use Psr\Http\Message\ResponseInterface;
use Zms\HyperfValidateAnnotations\Validate;

#[
    Controller(prefix: "/admin-api/administer-department", server: self::SERVER),
    Auth(guard: self::GUARD, label: ['后台管理', '部门'], app: self::APP)
]
class DepartmentController extends BasicsController
{
    #[Inject]
    public DepartmentService $service;


    #[
        GetMapping('data'),
        Auth(label: ['查看,管理'], sign: 'administer.department.data', view: '/administer/department/index')
    ]
    public function data(): ResponseInterface
    {
        $data = $this->service->getDepartmentData();
        return Response::Api()->success(data: $data);
    }


    #[
        RequestMapping('created', ['POST', 'GET']),
        Auth(sign: 'administer.department.created', logger: "POST"),
        ElemenuForm(DepartmentService::class, 'createdForm', param: ["pid"]),
        Validate(class: DepartmentValidate::class, scene: "created")
    ]
    public function created(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->service->created($data)) {
            return Response::Api()->success('角色添加成功');
        }
        return Response::Api()->fail('角色添加失败', $data);
    }


    #[
        RequestMapping('updated', ['POST', 'GET']),
        Auth(sign: 'administer.department.updated', logger: "POST", record: ['id', 'name']),
        ElemenuForm(class: DepartmentService::class, function: "updatedForm", param: ["id"]),
        Validate(class: DepartmentValidate::class, scene: "updated")
    ]
    public function updated(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->service->updated($data)) {
            return Response::Api()->success('部门编辑成功');
        }
        return Response::Api()->fail('部门编辑失败');
    }


    #[
        PostMapping('delete'),
        Auth(sign: 'administer.department.delete', describe: '删除失败'),
    ]
    public function delete(): ResponseInterface
    {
        $id = (int)$this->request->input('id', 0);
        if (!$id) {
            return Response::Api()->fail('请选择要删除的部门');
        }
        if ($this->service->exists(['pid' => $id])) {
            return Response::Api()->fail('该部门下有下级部门,请先删除下级部门');
        }
        if(container(UserService::class)->exists(['department_id'=>$id])){
            return Response::Api()->fail('该部门下还有员工,不能删除');
        }
        $result = $this->service->search(['id' => $id])->delete();
        if ($result) {
            return Response::Api()->success('部门已删除');
        }
        return Response::Api()->fail('删除失败');
    }
}